package org.fin.homeworkspringboot.pojo.vo;

import org.springframework.data.domain.Page;

import java.util.Objects;

public class PageModel {

  /** 当前页，0代表第一页 */
  private int page;

  /** 总页数 */
  private int total;

  /** 页面大小 */
  private int size;

  /** 是否第一页 */
  private boolean isFirst;

  /** 是否最后一页 */
  private boolean isLast;

  // 根据Page对象构建一个PageModel对象
  public static PageModel of(Page<?> page) {
    Objects.requireNonNull(page, "page 不能为空");

    PageModel result = new PageModel();
    result.setPage(page.getPageable().getPageNumber());
    result.setSize(page.getPageable().getPageSize());
    result.setTotal(page.getTotalPages());
    result.setFirst(page.isFirst());
    result.setLast(page.isLast());
    return result;
  }

  public int getPage() {
    return page;
  }

  public void setPage(int page) {
    this.page = page;
  }

  public int getTotal() {
    return total;
  }

  public void setTotal(int total) {
    this.total = total;
  }

  public int getSize() {
    return size;
  }

  public void setSize(int size) {
    this.size = size;
  }

  public boolean isFirst() {
    return isFirst;
  }

  public void setFirst(boolean first) {
    isFirst = first;
  }

  public boolean isLast() {
    return isLast;
  }

  public void setLast(boolean last) {
    isLast = last;
  }
}
